package gov.nhso.prov.acc.apps.acc67;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import gov.nhso.prov.acc.beans.Acc67MenuBean;
import gov.nhso.prov.acc.beans.Acc67RoleBean;
import gov.nhso.prov.acc.beans.Acc67RoleMenuBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.UserInfo;
import gov.nhso.prov.acc.utils.Utility;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;
import com.isud.paging.PageBean;

@SuppressWarnings("rawtypes")
public class ACC670102 extends Utility {
	public String url = "/apps/acc67/ACC670102A.jsp";
	
	public ACC670102(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = request.getParameter("job");
		if (job.equals("init")) {
			this.clearLastRequest(request, "lastSearch");
			this.init(request);
		} else if (job.equals("add")) {
			this.add(request);
		} else if (job.equals("search")) {
			this.setLastRequest(request, "lastSearch");
			this.search(request);
		} else if (job.equals("save")) {
			this.save(request);
		} else if (job.equals("view")) {
			 this.view(request);
		} else if (job.equals("update")) {
			this.update(request);
		} else if (job.equals("delete")) {
			this.delete(request);
		} else if (job.equals("back")) {
			if (this.getLastRequest(request, "lastSearch")) {
				this.search(request);
			} else {
				this.init(request);
			}
		}
	}
	
	private String init(HttpServletRequest request) {
		this.initPage(request);
		return this.url;
	}
	
	private String search(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			PageBean pageBean = new PageBean();
			Acc67RoleBean roleBean = new Acc67RoleBean();
			Acc67RoleBean role = new Acc67RoleBean();
			this.beanUtil().copyRequest2Bean(request, pageBean);
			this.beanUtil().copyRequest2Bean(request, role);
			roleBean.setRoleName(this.quote("%" + role.getRoleName() + "%"));
			roleBean.setActive(this.quote("Y"));
			pageBean = dao.selectPage(roleBean, "", pageBean);
			if (pageBean.getPageData() == null) {
				this.message(request).setMsgCode("I001");
			}
			request.setAttribute("pageBean", pageBean);
			request.setAttribute("role", role);
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}

	private String add(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67MenuBean menu = new Acc67MenuBean();
			menu.setActive("'Y'");
			List menus = dao.selectList(menu, " ORDER BY MENU_GROUP ASC, MENU_SEQ ASC, MENU_ID ASC");
			request.setAttribute("menus", menus);
			request.setAttribute("job", "save");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670102B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String save(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			UserInfo userInfo = this.user(request);
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67RoleBean role = new Acc67RoleBean();
			Acc67RoleBean roleSet = new Acc67RoleBean();
			Acc67RoleMenuBean roleMenu = null;
			this.beanUtil().copyRequest2Bean(request, role);
			roleSet.setRoleId(this.genCode(dao).getRoleId());
			roleSet.setRoleName(this.quote(role.getRoleName()));
			roleSet.setCreatedBy(this.quote(userInfo.getUserId()));
			roleSet.setUpdatedBy(this.quote(userInfo.getUserId()));
			dao.insert(roleSet);
			role.setRoleId(roleSet.getRoleId());
			String[] arrMenuId = request.getParameterValues("menuId");
			String[] arrAuthS = request.getParameterValues("authS");
			String[] arrAuthI = request.getParameterValues("authI");
			String[] arrAuthU = request.getParameterValues("authU");
			String[] arrAuthD = request.getParameterValues("authD");
			for (int i = 0; i < arrAuthS.length; i++) {
				roleMenu = new Acc67RoleMenuBean();
				String menuId = dao.chkData(arrMenuId[i]);
				String authS = dao.chkData(arrAuthS[i]);
				String authI = dao.chkData(arrAuthI[i]);
				String authU = dao.chkData(arrAuthU[i]);
				String authD = dao.chkData(arrAuthD[i]);
				if (authS.equals("1")) {
					roleMenu.setRoleId(role.getRoleId());
					roleMenu.setMenuId(this.quote(menuId));
					roleMenu.setAuth(this.quote(authS + authI + authU + authD));
					roleMenu.setCreatedBy(this.quote(userInfo.getUserId()));
					roleMenu.setUpdatedBy(this.quote(userInfo.getUserId()));
					dao.insert(roleMenu);
				}
			}
			this.getMenuAuth(request, dao, role.getRoleId());
			Acc67MenuBean menu = new Acc67MenuBean();
			menu.setActive("'Y'");
			List menus = dao.selectList(menu, " ORDER BY MENU_GROUP ASC, MENU_SEQ ASC, MENU_ID ASC");
			request.setAttribute("role", role);
			request.setAttribute("menus", menus);
			request.setAttribute("job", "update");
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
			e.printStackTrace();
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670102B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String update(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			UserInfo userInfo = this.user(request);
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67RoleBean role = new Acc67RoleBean();
			Acc67RoleBean roleSet = new Acc67RoleBean();
			Acc67RoleMenuBean roleMenu = new Acc67RoleMenuBean();
			this.beanUtil().copyRequest2Bean(request, role);
			roleSet.setRoleId(role.getRoleId());
			roleSet.setRoleName(this.quote(role.getRoleName()));
			roleSet.setUpdatedBy(this.quote(userInfo.getUserId()));
			roleSet.setUpdatedDate(this.sysDate);
			dao.update(roleSet);
			String[] arrMenuId = request.getParameterValues("menuId");
			String[] arrAuthS = request.getParameterValues("authS");
			String[] arrAuthI = request.getParameterValues("authI");
			String[] arrAuthU = request.getParameterValues("authU");
			String[] arrAuthD = request.getParameterValues("authD");
			dao.delete(roleMenu, " AND ROLE_ID = " + role.getRoleId());
			for (int i = 0; i < arrAuthS.length; i++) {
				roleMenu = new Acc67RoleMenuBean();
				String menuId = dao.chkData(arrMenuId[i]);
				String authS = dao.chkData(arrAuthS[i]);
				String authI = dao.chkData(arrAuthI[i]);
				String authU = dao.chkData(arrAuthU[i]);
				String authD = dao.chkData(arrAuthD[i]);
				if (authS.equals("1")) {
					roleMenu.setRoleId(role.getRoleId());
					roleMenu.setMenuId(this.quote(menuId));
					roleMenu.setAuth(this.quote(authS + authI + authU + authD));
					roleMenu.setCreatedBy(this.quote(userInfo.getUserId()));
					roleMenu.setUpdatedBy(this.quote(userInfo.getUserId()));
					dao.insert(roleMenu);
				}
			}
			this.getMenuAuth(request, dao, role.getRoleId());
			Acc67MenuBean menu = new Acc67MenuBean();
			menu.setActive("'Y'");
			List menus = dao.selectList(menu, " ORDER BY MENU_GROUP ASC, MENU_SEQ ASC, MENU_ID ASC");
			request.setAttribute("role", role);
			request.setAttribute("menus", menus);
			request.setAttribute("job", "update");
			this.message(request).setMsgCode("I002");
			
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670102B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String view(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67RoleBean role = new Acc67RoleBean();
			this.beanUtil().copyRequest2Bean(request, role);
			String roleId = role.getRoleId();
			dao.select(role);
			this.getMenuAuth(request, dao, roleId);
			Acc67MenuBean menu = new Acc67MenuBean();
			menu.setActive("'Y'");
			List menus = dao.selectList(menu, " ORDER BY MENU_GROUP ASC, MENU_SEQ ASC, MENU_ID ASC");
			request.setAttribute("role", role);
			request.setAttribute("menus", menus);
			request.setAttribute("job", "update");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670102B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String delete(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			UserInfo userInfo = this.user(request);
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67RoleBean role = new Acc67RoleBean();
			Acc67RoleMenuBean roleMenu = new Acc67RoleMenuBean();
			this.beanUtil().copyRequest2Bean(request, role);
			role.setRoleId(role.getRoleId());
			role.setActive("'N'");
			role.setUpdatedBy(this.quote(userInfo.getUserId()));
			role.setUpdatedDate(this.sysDate);
			dao.update(role);
			dao.delete(roleMenu, " AND ROLE_ID = " + role.getRoleId());
			this.message(request).setMsgCode("I003");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.getLastRequest(request, "lastSearch");
				this.url = this.search(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private void getMenuAuth(HttpServletRequest request, ISUD dao, String roleId) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT A.MENU_ID, B.AUTH ");
		sql.append("FROM " + this.schema + ".ACC67_MENU A ");
		sql.append("JOIN " + this.schema + ".ACC67_ROLE_MENU B ");
		sql.append("ON A.MENU_ID = B.MENU_ID ");
		sql.append("WHERE B.ROLE_ID = " + roleId + " ");
		sql.append("AND A.ACTIVE = 'Y' ");
		sql.append("AND B.ACTIVE = 'Y' ");
		sql.append("ORDER BY A.MENU_GROUP ASC, A.MENU_SEQ ASC");
		List items = dao.listDataSql(sql.toString());
		Map<String, String> menuAuth = new HashMap<String, String>();
		for (int i = 0; i < items.size(); i++) {
			Map data = (Map) items.get(i);
			menuAuth.put(dao.chkData(data.get("MENU_ID")), this.sysAuth(request).authBit2Str(dao.chkData(data.get("AUTH"))));
		}
		request.setAttribute("roleAuth", menuAuth);
	}
}
